//
//  AppController.m
//  TextTracks
//
//  Created by Sebas on 06/11/07.
//  Copyright 2007 Sebas Corp. All rights reserved.
//

#import "AppController.h"

@interface AppController (Private)

@end

@implementation AppController

#pragma mark -
#pragma mark Private Methods



-(void)fileToTextRracks:(NSString *)theFile
{
	
	NSString *fileContent = [NSString stringWithContentsOfFile:theFile];
	
	
	srObject = [[SubRip alloc] initWithString:fileContent];
	
	[self setCollection:[srObject itemsCollection]];
	
}


#pragma mark -
#pragma mark IBActions


- (IBAction)openFile:(id)sender
{
	NSArray *fileTypes = [NSArray arrayWithObjects:@"srt", @"txt",
						  @"sub", nil];
		
	NSOpenPanel* oPanel = [NSOpenPanel openPanel];
	
	[oPanel setCanChooseDirectories:NO];
	[oPanel setCanChooseFiles:YES];
	[oPanel setCanCreateDirectories:YES];
	[oPanel setAllowsMultipleSelection:NO];
	[oPanel setAlphaValue:0.95];
	[oPanel setTitle:@"Select a file to open"];
	
	if([oPanel runModalForDirectory:nil file:nil types:fileTypes] == NSOKButton)
	{
		NSArray* files = [oPanel filenames];
		
		NSString* fileName = [files objectAtIndex:0];
		
		[self fileToTextRracks:fileName];
	}
	
}


-(IBAction)saveFile:(id)sender
{
		
	NSMutableString* temp = [[NSMutableString alloc] init];
	
	
	for(SubRipItem *item  in collection)
	{
		[temp appendString:[NSString stringWithFormat:@"%@\r\n", item.subtitleNumber]];
		[temp appendString:[NSString stringWithFormat:@"%@ --> %@\r\n", item.startTime, item.endTime]];
		[temp appendString:[NSString stringWithFormat:@"%@\r\n", item.textOfSubtitle]];
	}		

	
	
	NSSavePanel* aPanel = [NSSavePanel savePanel];
	if( [aPanel runModal] == NSFileHandlingPanelOKButton )
	{
		[temp writeToFile:[aPanel filename] atomically:YES encoding:NSMacOSRomanStringEncoding error:nil];
	}
}

-(IBAction)addTrack:(id)sender
{
	
		
	[srObject adjustTime:[textOffset floatValue]];
	
}

#pragma mark -
#pragma mark Initialization

-(id)init
{
	if (self = [super init])
	{
		collection = [[NSMutableArray alloc] init];

/*		textTracks = [[NSMutableString alloc] init];
		properties = [[NSMutableDictionary alloc]
					  initWithObjects:[NSArray arrayWithObjects:@"552", @"40", @"20", @"On",@"On",@"1000",
									   @"absolute",@"6",@"0",@"0",@"Off",@"Off",@"Off", @"Off", @"Off", @"Off",
									   @"Helvetica", @"On", @"65535, 61695, 17407", @"center", nil] 
					  forKeys:[NSArray arrayWithObjects:@"width", @"height", @"size", @"anti-alias",
							   @"keyedText", @"timeScale", @"timeStamps", @"language", @"textEncoding",
							   @"ScrollDelay", @"ScrollIn", @"ScrollOut", @"HorizontalScroll", @"ReverseScroll",
							   @"ContinuousScroll", @"FlowHorizontal", @"font", @"shrinkTextBox", @"textColor",
							   @"justify", nil]];
*/		
	}
	
	return self;
	
}

#pragma mark -
#pragma mark Public Methods

-(NSMutableArray *)collection
{
	return collection;
}

-(void)setCollection:(NSMutableArray *)aCollection
{
	collection = [aCollection copy];
}

- (void)windowWillClose:(NSNotification *)aNotification {
    [NSApp performSelector:@selector(terminate:) 
				withObject:self 
				afterDelay: 0.5];
}

#pragma mark -
#pragma mark AppDelegate Methods

- (BOOL)application:(NSApplication *)sender openFile:(NSString *)path 
{ 
	NSString *ext = [[NSString stringWithString:path] pathExtension];
		
	if([ext isEqualToString: @"srt"] || [ext isEqualToString: @"txt"] || [ext isEqualToString: @"sub"])
		[self fileToTextRracks:path];
	
	return YES; 
} 


@end
